package com.xing.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.UUID;

import android.R.bool;
import android.R.integer;
import android.R.string;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.JsonObject;
import com.xing.db.DBService;
import com.xing.model.HsdAccount;
import com.xing.model.HsdDataResearch;
import com.xing.model.HsdDataResult;
import com.xing.model.HsdModelAnswer;
import com.xing.model.HsdModelQuestion;
import com.xing.model.HsdModelSurvey;
import com.xing.model.HsdQuestionAndAnswerModelListObj;
import com.xing.model.HsdUploadDetail;
import com.xing.model.UploadDetail;
import com.xing.my_model.SurveyListEntity;
import com.xing.util.Common;
import com.xing.util.MyLog;

public class DBDao {
	/**
	 * 
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static List<HsdModelSurvey> getHsdModelSurveyList(Context context) throws Exception{
		List<HsdModelSurvey> list = new ArrayList<HsdModelSurvey>();
//		SQLiteDatabase db=Common.OpenDataBase(context);
		DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
		SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
		Cursor c = db.rawQuery("select * from "+DBService.HsdModelSurvey, new String[]{});  
        while (c.moveToNext()) {
            String name = c.getString(c.getColumnIndex("name"));  
            HsdModelSurvey surveyItemObject = new HsdModelSurvey();
            surveyItemObject.setStatus(c.getInt(c.getColumnIndex("status")));
            surveyItemObject.setSurveyuuid(c.getString(c.getColumnIndex("surveyuuid")));
            surveyItemObject.setName(name);
    		list.add(surveyItemObject);
        }  
        c.close();
        db.close();
		return list;
	}
	
	/**
	 * 得到问卷的总题数
	 * @param context
	 * @param surveyuuid
	 * @return
	 * @throws Exception
	 */
	public static int getHsdModeSurveyQuestoinNum(Context context,String surveyuuid) throws Exception{
		int questionCount = -1;
//		SQLiteDatabase db=Common.OpenDataBase(context);
		   DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
		   SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
		String sqlString = "select * from "+DBService.HsdModelSurvey +" where surveyuuid = ?";
		Cursor c = db.rawQuery(sqlString, new String[]{surveyuuid});
		while (c.moveToNext()) {
			questionCount = c.getInt(c.getColumnIndex("questioncount"));
		}
		 c.close();
		db.close();
		return questionCount;
	}
	/**得到问卷问题模型信息
	 * @param 
	 * @param context  
	 * @return
	 */
	public static HsdModelQuestion getHsdModelQuestionObjec(String surveyuuid,String currentSortIndex,Context context) throws Exception{
		
//	    SQLiteDatabase db=Common.OpenDataBase(context);
		DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
		   SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
		Cursor c = db.rawQuery("select * from questionnaire_question_model_info where surveyuuid = ? and sortindex = ? and isgridchildquestion = 0", new String[]{surveyuuid,currentSortIndex});  
		 HsdModelQuestion surveyDetailObject = new HsdModelQuestion();
		 while (c.moveToNext()) {
	            String description = c.getString(c.getColumnIndex("description"));  
	            String questionuuid = c.getString(c.getColumnIndex("questionuuid"));
	            int type =  c.getInt(c.getColumnIndex("type"));
	            int typeid = c.getInt(c.getColumnIndex("typeid"));
	            int sortindex = c.getInt(c.getColumnIndex("sortindex"));
	            surveyDetailObject.setDescription(description);
	            surveyDetailObject.setQuestionuuid(questionuuid);
	            surveyDetailObject.setType(type);
	            surveyDetailObject.setTypeid(typeid);
	            surveyDetailObject.setSortindex(sortindex);
	            surveyDetailObject.setSurveyuuid(surveyuuid);
	      }  
		 c.close();
		 db.close();
		 return surveyDetailObject;
	}
	
	/**得到问卷问题模型信息
	 * @param 
	 * @param context  
	 * @return
	 */
	public static List<HsdModelQuestion> getChildHsdModelQuestionObjec(String surveyuuid,String fatherquestionuuid,Context context) throws Exception{
		List<HsdModelQuestion> list = new ArrayList<HsdModelQuestion>(); 
		DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
		   SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
		Cursor c = db.rawQuery("select * from questionnaire_question_model_info where surveyuuid = ?  and fatherquestionuuid = ?and isgridchildquestion = 1 order by sortindex", new String[]{surveyuuid,fatherquestionuuid});  
		 HsdModelQuestion surveyDetailObject = null;
		 while (c.moveToNext()) {
			    surveyDetailObject = new HsdModelQuestion();
	            String description = c.getString(c.getColumnIndex("description"));  
	            String questionuuid = c.getString(c.getColumnIndex("questionuuid"));
	            int type =  c.getInt(c.getColumnIndex("type"));
	            int typeid = c.getInt(c.getColumnIndex("typeid"));
	            int sortindex = c.getInt(c.getColumnIndex("sortindex"));
	            
	            surveyDetailObject.setDescription(description);
	            surveyDetailObject.setQuestionuuid(questionuuid);
	            surveyDetailObject.setFatherquestionuuid(fatherquestionuuid);
	            surveyDetailObject.setType(type);
	            surveyDetailObject.setTypeid(typeid);
	            surveyDetailObject.setSortindex(sortindex);
	            surveyDetailObject.setSurveyuuid(surveyuuid);
	            
	            list.add(surveyDetailObject);
	      }  
		 c.close();
		 db.close();
		 return list;
	}
	
	public static List<HsdModelAnswer> getHsdModelAnswerList(String questionuuid,String surveyuuid,Context context) throws Exception{
		List<HsdModelAnswer> list = new ArrayList<HsdModelAnswer>();
		DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
		SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
		MyLog.outLogDetail(questionuuid+"-----------"+surveyuuid);
		Cursor c = db.rawQuery("select * from question_answer_model_info where questionuuid = ? and surveyuuid = ?", new String[]{questionuuid,surveyuuid});  
        while (c.moveToNext()) {
            String name = c.getString(c.getColumnIndex("name"));  
            String answeruuid = c.getString(c.getColumnIndex("answeruuid"));
            HsdModelAnswer  hsdModelAnswer = new HsdModelAnswer();
            hsdModelAnswer.setName(name);
            hsdModelAnswer.setAnsweruuid(answeruuid);
            hsdModelAnswer.setQuestionuuid(questionuuid);
            list.add(hsdModelAnswer);
        }  
        c.close();
        db.close();
		return list;
	}
	
	/**
	 * 插入问题模型和答案模型 list数据
	 * @throws Exception
	 */
	public static boolean insertHsdQuestionAndAnswerModelListObj(Context context,HsdQuestionAndAnswerModelListObj hsdQuestionModelListObj) throws Exception{
//		SQLiteDatabase db=Common.OpenDataBase(context);
		DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
		SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
		boolean isInsertSucess = false;
		
			HsdModelSurvey hsdModelSurvey = hsdQuestionModelListObj.survey;
			ContentValues hsdModelSurveyCV = new ContentValues();  
			hsdModelSurveyCV.put("surveyuuid", hsdModelSurvey.getSurveyuuid());
			hsdModelSurveyCV.put("name", hsdModelSurvey.getName());
			hsdModelSurveyCV.put("subname", hsdModelSurvey.getSubname());
			hsdModelSurveyCV.put("description",hsdModelSurvey.getDescription());
			hsdModelSurveyCV.put("descriptiontext", hsdModelSurvey.getDescriptiontext());
			hsdModelSurveyCV.put("createtype", hsdModelSurvey.getCreatetype());
			hsdModelSurveyCV.put("type", hsdModelSurvey.getType());
			hsdModelSurveyCV.put("logo", hsdModelSurvey.getLogo());
			hsdModelSurveyCV.put("progressbar", hsdModelSurvey.getProgressbar());
			hsdModelSurveyCV.put("pagecount", hsdModelSurvey.getPagecount());
			hsdModelSurveyCV.put("questioncount", hsdModelSurvey.getQuestioncount());
			hsdModelSurveyCV.put("timeschedule", hsdModelSurvey.getTimeschedule());
			hsdModelSurveyCV.put("starttime", hsdModelSurvey.getStarttime());
			hsdModelSurveyCV.put("endtime", hsdModelSurvey.getEndtime());
			hsdModelSurveyCV.put("subname", hsdModelSurvey.getSubname());
			hsdModelSurveyCV.put("createtime", hsdModelSurvey.getCreatetime());
			hsdModelSurveyCV.put("creatoraccount", hsdModelSurvey.getCreatoraccount());
			hsdModelSurveyCV.put("creatorname", hsdModelSurvey.getCreatorname());
			hsdModelSurveyCV.put("firstsavetime", hsdModelSurvey.getFirstsavetime());
			hsdModelSurveyCV.put("modifytime", hsdModelSurvey.getModifytime());
			/**
			 * 插入之前改为已下载状态
			 */
			hsdModelSurveyCV.put("status",2);
			db.insert(DBService.HsdModelSurvey, null, hsdModelSurveyCV);
		
		for (int i = 0; i < hsdQuestionModelListObj.questionlist.size(); i++) {
			HsdModelQuestion hsdModelQuestion = hsdQuestionModelListObj.questionlist.get(i);
		    ContentValues cv = new ContentValues();  
		    cv.put("questionuuid", hsdModelQuestion.getQuestionuuid());
	        cv.put("surveyuuid",hsdModelQuestion.getSurveyuuid());
	        cv.put("pageuuid", hsdModelQuestion.getPageuuid());
	        cv.put("sortindex", hsdModelQuestion.getSortindex());
	        cv.put("type", hsdModelQuestion.getType());
	        cv.put("typeid", hsdModelQuestion.getTypeid());
	        cv.put("description", hsdModelQuestion.getDescription());
	        cv.put("descriptionview",hsdModelQuestion.getDescriptionview());
	        cv.put("isgridchildquestion", hsdModelQuestion.getIsgridchildquestion());
	        cv.put("fatherquestionuuid", hsdModelQuestion.getFatherquestionuuid());
	        cv.put("format", hsdModelQuestion.getFormat());
	        cv.put("required", hsdModelQuestion.getRequired());
	        cv.put("imageurl", hsdModelQuestion.getImageurl());
	        cv.put("instruction", hsdModelQuestion.getInstruction());
	        cv.put("imagetype", hsdModelQuestion.getImagetype());
	        cv.put("showhasother", hsdModelQuestion.getShowhasother());
	        cv.put("rangeformat", hsdModelQuestion.getRangeformat());
	        cv.put("rangemax", hsdModelQuestion.getRangemax());
	        cv.put("rangemin", hsdModelQuestion.getRangemin());
	        cv.put("createtime", hsdModelQuestion.getCreatetime());
	        cv.put("modifytime", hsdModelQuestion.getModifytime());
	        db.insert(DBService.questionnaire_question_model_info, null, cv);
	}	
	
	for (int i = 0; i < hsdQuestionModelListObj.answerlist.size(); i++) {
		HsdModelAnswer hsdModelAnswer = hsdQuestionModelListObj.answerlist.get(i);
	    ContentValues cv = new ContentValues();  
	    cv.put("answeruuid", hsdModelAnswer.getAnsweruuid());
	    cv.put("surveyuuid",hsdModelAnswer.getSurveyuuid());
	    cv.put("questionuuid", hsdModelAnswer.getQuestionuuid());
	    cv.put("sortindex",hsdModelAnswer.getSortindex());  
        cv.put("name",hsdModelAnswer.getName());  
        cv.put("hasother", hsdModelAnswer.getHasother());
        cv.put("point", hsdModelAnswer.getPoint());
        cv.put("imagetype", hsdModelAnswer.getImagetype());
        cv.put("imageurl", hsdModelAnswer.getImageurl());
        cv.put("imageheight", hsdModelAnswer.getImageheight());
        cv.put("imagewidth", hsdModelAnswer.getImagewidth());
        cv.put("createtime", hsdModelAnswer.getCreatetime());
        db.insert(DBService.questionnaire_answer_model_info, null, cv);
	}	
		db.close();
		isInsertSucess = true;
		return isInsertSucess;
	}
	/**
	 * insert 问卷问卷的所选结果
	 * @param researchuuid
	 * @param surveyuuid
	 * @param answeruuid
	 * @param questionuuid
	 * @param answertext
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static boolean insertQuestionResult(String researchuuid,String surveyuuid,String answeruuid,String questionuuid,String answertext,Context context) throws Exception{
		boolean isInsertSucess = false;
		HsdDataResult hsdDataResult = new HsdDataResult();
		hsdDataResult.setSurveyuuid(surveyuuid);
		hsdDataResult.setAnsweruuid(answeruuid);
		hsdDataResult.setResearchuuid(researchuuid);
		hsdDataResult.setAnswertext(answertext);
		hsdDataResult.setQuestionuuid(questionuuid);
		
//		SQLiteDatabase db=Common.OpenDataBase(context);
		DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
		   SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
	    ContentValues cv = new ContentValues();  
	    cv.put("resultuuid", UUID.randomUUID().toString());
	    cv.put("surveyuuid",hsdDataResult.getSurveyuuid());
	    cv.put("answeruuid", hsdDataResult.getAnsweruuid());
	    cv.put("researchuuid", hsdDataResult.getResearchuuid());
	    cv.put("answertext", hsdDataResult.getAnswertext());
        cv.put("questionuuid",hsdDataResult.getQuestionuuid());  
        db.insert(DBService.questionnaire_result_info, null, cv);
        
        isInsertSucess = true;
        db.close();
        return isInsertSucess;
	}
	
	/**
	 * 删除问题结果表
	 * @param researchuuid
	 * @param surveyuuid
	 * @param answeruuid
	 * @param answertext
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static boolean deleteQuestionResult(String researchuuid,String surveyuuid,String answeruuid,Context context) throws Exception{
		boolean isDeleteSucess = false;
//		SQLiteDatabase db=Common.OpenDataBase(context);
		DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
		   SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
	    db.delete(DBService.questionnaire_result_info, "researchuuid = ? and surveyuuid = ? and answeruuid = ?", new String[]{researchuuid,surveyuuid,answeruuid});
		isDeleteSucess = true;
		db.close();
		return isDeleteSucess;
	}
	
	/**
	 * 更新结果表数据
	 * @param researchuuid
	 * @param surveyuuid
	 * @param answeruuid
	 * @param questionuuid
	 * @param answertext
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static boolean updateQuestionResult(String researchuuid,String surveyuuid,String answeruuid,String questionuuid,String answertext,Context context) throws Exception{
		boolean isUpdateSucess = false;
//		SQLiteDatabase db=Common.OpenDataBase(context);
		DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
		SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
		
		HsdDataResult hsdDataResult = new HsdDataResult();
		hsdDataResult.setSurveyuuid(surveyuuid);
		hsdDataResult.setAnsweruuid(answeruuid);
		hsdDataResult.setResearchuuid(researchuuid);
		hsdDataResult.setAnswertext(answertext);
		hsdDataResult.setQuestionuuid(questionuuid);
       
	    ContentValues updateContentValues = new ContentValues();
	    updateContentValues.put("answeruuid", hsdDataResult.getAnsweruuid());
	    updateContentValues.put("answertext", hsdDataResult.getAnswertext());
     
	    db.update(DBService.questionnaire_result_info, updateContentValues," researchuuid = ? and surveyuuid = ? and questionuuid = ?",new String[]{researchuuid,surveyuuid,questionuuid});
	    isUpdateSucess = true;
	    db.close();
	    return isUpdateSucess;
	}
	
	/**
	 * 得到问题问卷问题的所选结果List(问卷详情页结果显示）,这个ｌｉｓｔ就一个对象。
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static List<HsdDataResult> getHsdDataResultList(String researchuuid,String surveyuuid,String questionuuid,Context context) throws Exception{
		List<HsdDataResult> list = new ArrayList<HsdDataResult>();
//		SQLiteDatabase db=Common.OpenDataBase(context);
		DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
		   SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
		Cursor c = db.rawQuery("select * from questionnaire_result_model where researchuuid = ? and questionuuid = ? and surveyuuid = ? ",
				new String[]{researchuuid,questionuuid,surveyuuid});  
        while (c.moveToNext()) {
        	HsdDataResult hsdDataResult = new HsdDataResult();
        	String answertext = c.getString(c.getColumnIndex("answertext"));
        	String answeruuid = c.getString(c.getColumnIndex("answeruuid"));
        	hsdDataResult.setAnswertext(answertext);
        	hsdDataResult.setAnsweruuid(answeruuid);
        	list.add(hsdDataResult);
        } 
        c.close();
        db.close();
		return list;
	}
	
	/**
	 * 得到本地问卷的uuid list，然后传到服务器
	 * @param context
	 * @return
	 * @throws Exception
	 */
   public static List<String> getHsdModelSurveyuuidList(Context context) throws Exception{
		List<String> surveyuuidList =  new ArrayList<String>();
//		SQLiteDatabase db=Common.OpenDataBase(context);
		DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
		   SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
		Cursor c = db.rawQuery("select * from "+DBService.HsdModelSurvey,null);  
        while (c.moveToNext()) {
        	String surveyuuid = c.getString(c.getColumnIndex("surveyuuid"));
        	surveyuuidList.add(surveyuuid);
        }  
        c.close();
        db.close();
		return surveyuuidList;
	}
   
   /**
    * 得到文件的完成情况详情信息
    * @param context
    * @param surveyuuid
    * @param hasFinishedView
    * @param notFinishedView
    * @return
    * @throws Exception
    */
   public static SurveyListEntity getSurveyListEntity(Context context,String surveyuuid,View hasFinishedView,View notFinishedView) throws Exception{
	    SurveyListEntity surveyListEntity =  new SurveyListEntity();
//		SQLiteDatabase db=Common.OpenDataBase(context);
	    DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
	    SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
		Cursor c = db.rawQuery("select * from "+DBService.HsdDataResearch+" where surveyuuid = ?",new String[]{surveyuuid}); 
		int hasFinished = 0;
		int notFinished = 0;
        while (c.moveToNext()) {
        	/**
        	 * 1 正要调查   2:已完成
        	 */
        	int status = c.getInt(c.getColumnIndex("status"));
        	if (status == 1) {
        		notFinished++;
			}else {
				hasFinished++;
			}
        }  
        surveyListEntity.setHasFinished(hasFinished);
    	surveyListEntity.setNotFinished(notFinished);
    	surveyListEntity.setHasFinishedView(hasFinishedView);
    	surveyListEntity.setNotFinishedView(notFinishedView);
    	c.close();
    	db.close();
	   return surveyListEntity;
   }
   /**
    * 得到本地文件的上传状态
    * @param context
    * @param has_uploaded_view
    * @param not_upload_view
    * @param surveyuuid
    * @return
    * @throws Exception
    */
   public static UploadDetail getResearchUpStatus(Context context,View has_uploaded_view,View not_upload_view,String surveyuuid) throws Exception{
	   UploadDetail uploadDetail = new UploadDetail();
//	   SQLiteDatabase db=Common.OpenDataBase(context);
	   DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
	   SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
	   Cursor c = db.rawQuery("select * from "+DBService.HsdDataResearch+" where surveyuuid = ?",new String[]{surveyuuid});  
	   int hasUploaded = 0;
	   int notUpload = 0;
       while (c.moveToNext()) {
        	int has_upload = c.getInt(c.getColumnIndex("has_uploaded"));
        	if (has_upload == 1) {
        		hasUploaded++;
			}else {
				notUpload++;
			}
        }  
       View view1 = not_upload_view;
       View view2 = has_uploaded_view;
       uploadDetail.setHasUploaded(hasUploaded);
       uploadDetail.setNotUpload(notUpload);
       uploadDetail.setNotuploadView(view1);
       uploadDetail.setHasUploadedView(view2);
       c.close();
       db.close();
	   return uploadDetail;
   }
   
   
   /**
    * 得到未上传的问卷的researchlist，然后将这些上传到服务器
    * @param context
    * @param surveyuuid
    * @return
    * @throws Exception
    */
   public static List<HsdDataResearch> getHsdDataResearchNotUploadList(Context context,String surveyuuid) throws Exception{
	   List<HsdDataResearch> list = new ArrayList<HsdDataResearch>();
//	   SQLiteDatabase db = Common.OpenDataBase(context);
	   
	   DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
	   SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
	   
	   Cursor c = db.rawQuery("select * from "+DBService.HsdDataResearch+" where surveyuuid = ? and has_uploaded = 0 and status = 2",new String[]{surveyuuid}); 
//	   Cursor c = db.rawQuery("select * from "+DBService.HsdDataResearch+" where surveyuuid = ?",new String[]{surveyuuid});
       while (c.moveToNext()) {
    	   HsdDataResearch hsdDataResearch = new HsdDataResearch();
           String name= c.getString(c.getColumnIndex("name"));
           String researchuuid= c.getString(c.getColumnIndex("researchuuid"));
           String currentSurveyuuid = surveyuuid;
           hsdDataResearch.setSex(c.getInt(c.getColumnIndex("sex")));
           hsdDataResearch.setAge(c.getInt(c.getColumnIndex("age")));
           hsdDataResearch.setMobilephone(c.getString(c.getColumnIndex("mobilephone")));
           hsdDataResearch.setCreatetime(c.getString(c.getColumnIndex("createtime")));
           hsdDataResearch.setModifytime(c.getString(c.getColumnIndex("modifytime")));
           hsdDataResearch.setResearchname(c.getString(c.getColumnIndex("researchname")));
           hsdDataResearch.setName(name);
           hsdDataResearch.setResearchuuid(researchuuid);
           hsdDataResearch.setSurveyuuid(currentSurveyuuid);
           list.add(hsdDataResearch);
        }  
       c.close();
       db.close();
	   return list;
   }
   
   /**
    * 查询要向服务回上传的结果数据对象
    * @param context
    * @param hsdUploadResearch
    * @param surveyuuid
    * @param researchuuid
    * @return
    * @throws Exception
    */
   public static HsdUploadDetail getHsdUploadDetail(Context context,HsdDataResearch hsdDataResearch,String surveyuuid,String researchuuid) throws Exception{
	   HsdUploadDetail hsdUploadDetail = new HsdUploadDetail();
	   List<HsdDataResult> list = new ArrayList<HsdDataResult>();
//	   SQLiteDatabase db= Common.OpenDataBase(context);
	   DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
	   SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
	   Cursor c = db.rawQuery("select * from questionnaire_result_model where surveyuuid = ? and researchuuid = ?",new String[]{surveyuuid,researchuuid});  
       while (c.moveToNext()) {
    	   HsdDataResult hsdDataResult = new HsdDataResult();
           String resultuuid= c.getString(c.getColumnIndex("resultuuid"));
           String currentResearchuuid =  c.getString(c.getColumnIndex("researchuuid"));
           String currentSurveyuuid = c.getString(c.getColumnIndex("surveyuuid"));
           String fatherquestionuuid= c.getString(c.getColumnIndex("fatherquestionuuid"));
           String questionuuid= c.getString(c.getColumnIndex("questionuuid"));
           String answeruuid= c.getString(c.getColumnIndex("answeruuid"));
           String answertext= c.getString(c.getColumnIndex("answertext"));
           String createtime= c.getString(c.getColumnIndex("createtime"));
           String modifytime= c.getString(c.getColumnIndex("modifytime"));
           
           hsdDataResult.setResultuuid(resultuuid);
           hsdDataResult.setResearchuuid(currentResearchuuid);
           hsdDataResult.setSurveyuuid(currentSurveyuuid);
           hsdDataResult.setFatherquestionuuid(fatherquestionuuid);
           hsdDataResult.setQuestionuuid(questionuuid);
           hsdDataResult.setAnsweruuid(answeruuid);
           hsdDataResult.setAnswertext(answertext);
           hsdDataResult.setCreatetime(createtime);
           hsdDataResult.setModifytime(modifytime);
           list.add(hsdDataResult);
        }  
       hsdUploadDetail.setHsdDataResearch(hsdDataResearch);
       hsdUploadDetail.setResultsList(list);
       c.close();
       db.close();
	   
	   return hsdUploadDetail;
   }
   
   
   /**
    * 在列表页 显示的
    * @param context
    * @param surveyuuid
    * @return
    * @throws Exception
    */
   public static List<HsdDataResearch> getHsdDataResearchListInDetail(Context context,String surveyuuid) throws Exception{
	   List<HsdDataResearch> list = new ArrayList<HsdDataResearch>();
//	   SQLiteDatabase db=Common.OpenDataBase(context);
	   DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
	   SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
	   Cursor c = db.rawQuery("select * from "+DBService.HsdDataResearch+" where surveyuuid = ? order by _id desc", new String[]{surveyuuid});  
       while (c.moveToNext()) {
           String name = c.getString(c.getColumnIndex("name"));  
           HsdDataResearch secondItemObject = new HsdDataResearch();
           secondItemObject.setName(name);
           secondItemObject.setResearchuuid(c.getString(c.getColumnIndex("researchuuid")));
           secondItemObject.setModifytime(c.getString(c.getColumnIndex("modifytime"))) ;
           secondItemObject.setStatus( c.getInt(c.getColumnIndex("status")));
           secondItemObject.setSurveyuuid(c.getString(c.getColumnIndex("surveyuuid")));
           list.add(secondItemObject);
       }  
       c.close();
       db.close();
	   return list;
   }
   
   /**
    * 更新research table 的上传状态
    * @param context
    * @param surveyuuid
    * @param researchuuid
    * @return
    * @throws Exception
    */
   public static boolean updateHsdDataResearchUpStatus(Context context,String surveyuuid,String researchuuid) throws Exception{
//	   SQLiteDatabase db=Common.OpenDataBase(context);
	   DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
	   SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
	   boolean isUpdateSucess = false;
	    ContentValues updateContentValues = new ContentValues();
	    updateContentValues.put("has_uploaded", 1);
    
	    db.update(DBService.HsdDataResearch, updateContentValues," researchuuid = ? and surveyuuid = ?",new String[]{researchuuid,surveyuuid});
	    isUpdateSucess = true;
	    db.close();
	    return isUpdateSucess;
   }
   
   /**
     * 更新本地问卷状态
	 * 问卷状态
	 * 1 not download
	 * 2:has downloaded
	 * 3:out of date
	 * 4:has deleted
	 *
    * @param context
    * @param surveyuuid
    * @return
    * @throws Exception
    */
   public static boolean updateHsdModelSurveyStatus(Context context,String surveyuuid,int status) throws Exception{
//	   SQLiteDatabase db=Common.OpenDataBase(context);
	   DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
	   SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
	   boolean isUpdateSucess = false;
	    
	   ContentValues updateContentValues = new ContentValues();
	   updateContentValues.put("status", status);
	   db.update(DBService.HsdModelSurvey, updateContentValues," surveyuuid = ?",new String[]{surveyuuid});
	   isUpdateSucess = true;
	   db.close();
	   return isUpdateSucess;
   }
   
   /**
    * 提交问卷
    * @param context
    * @param surveyuuid
    * @param researchuuid
    * @return
    * @throws Exception
    */
   public static boolean updateHsdDataResearchFinishStatus(Context context,String surveyuuid,String researchuuid) throws Exception{
//	   SQLiteDatabase db=Common.OpenDataBase(context);
	   DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
	   SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
	    boolean isUpdate = false;
	    ContentValues updateContentValues = new ContentValues();
	    updateContentValues.put("status", 2);
	    db.update(DBService.HsdDataResearch, updateContentValues," researchuuid = ? and surveyuuid = ?",new String[]{researchuuid,surveyuuid});
	    isUpdate = true;
	    db.close();
	    return isUpdate;
   }

   /**
    * 
    * @param context
    * @param surveyuuid
    * @param name
    * @param sex
    * @param age
    * @param telephone
    * @param mobilephone
    * @param email
    * @param documenttype
    * @param idnumber
    * @param researchaccount
    * @param researchname
    * @param createtime
    * @param modifytime
    * @return
    * @throws Exception
    */
   public static boolean insertHsdDataResearch(Context context,String researchuuid,String surveyuuid,String name,int sex,int age,String telephone,String mobilephone
		   ,String email,int documenttype,String idnumber,String researchaccount,String researchname,String createtime,String modifytime) throws Exception{
//	   SQLiteDatabase db=Common.OpenDataBase(context);
	   DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
	   SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
	   boolean isInsert = false;
	    ContentValues cv = new ContentValues(); 
	    cv.put("researchuuid", researchuuid);
	    cv.put("surveyuuid",surveyuuid);
	    cv.put("name",name);
	    cv.put("sex",sex);
	    cv.put("age",age);
	    cv.put("telephone",telephone);
	    cv.put("mobilephone",mobilephone);
	    cv.put("email", email);
	    cv.put("documenttype",documenttype);
	    cv.put("idnumber",idnumber);
	    cv.put("researchaccount",researchaccount);
	    cv.put("researchname",researchname);
	    cv.put("createtime",createtime);
	    cv.put("modifytime",modifytime);
	    /**
	     * 正要调查状态
	     */
	    cv.put("status", 1);
	    cv.put("has_uploaded", 0);
	    
       db.insert(DBService.HsdDataResearch, null, cv);
       isInsert = true;
       db.close();
       return isInsert;
   }
   
   public static boolean insertHsdAccount(Context context,String username,String password,String loginid,String token) throws Exception{
	   DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
	   SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
	   boolean isInsert = false;
	   ContentValues cv = new ContentValues(); 
	    cv.put("username",username);
	    cv.put("password",password);
	    cv.put("loginid", loginid);
	    cv.put("token",token);
	    
       db.insert(DBService.HSD_ACCOUNT, null, cv);
       isInsert = true;
       db.close();
       return isInsert;
   }
   
   public static HsdAccount getHsdAccount(Context context,String loginid) throws Exception{
	   DBService databaseOpenhelper=new DBService(context,DBService.dataBaseName, null, DBService.currentVersion);
	   SQLiteDatabase db = databaseOpenhelper.getWritableDatabase();
	   Cursor c = db.rawQuery("select * from "+DBService.HSD_ACCOUNT+" where loginid = ?", new String[]{loginid});  
	   HsdAccount hsdAccount = null;
       while (c.moveToNext()) {
    	   hsdAccount = new HsdAccount();
    	   hsdAccount.setApptoken(c.getString(c.getColumnIndex("token")));
    	   hsdAccount.setUsername(c.getString(c.getColumnIndex("username")));  
    	   hsdAccount.setPassword(c.getString(c.getColumnIndex("password")));  
       }  
       c.close();
       db.close();
	   return hsdAccount;
   }
   
}

